package com.xhsj.user.pms.overseas.service;

import com.google.common.base.Joiner;
import com.xhsj.user.hr.base.BaseService;
import com.xhsj.user.pms.overseas.dto.MonitorListDto;
import com.xhsj.user.pms.overseas.entity.TbPamOverseasRegionalDetails;
import com.xhsj.user.pms.overseas.mapper.TbPamOverseasRegionalDetailsMapper;
import com.xhsj.user.pms.overseas.query.TbPamOverseasRegionalDetailsQuery;
import com.xhsj.user.pms.overseas.query.TimeLimitNameQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.pms.patent.mapper.TbPamOverseasApplyMapper;
import org.springframework.stereotype.Service;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
/**
 * <p>
 * 专利模块海外地区时间详情表 服务实现类
 * </p>
 *
 * @author liuqinghua
 * @since Jan 22, 2021 11:41:13 AM
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbPamOverseasRegionalDetails")
public class TbPamOverseasRegionalDetailsService extends BaseService<TbPamOverseasRegionalDetailsMapper, TbPamOverseasRegionalDetails> {
    @Autowired
    private TbPamOverseasRegionalDetailsMapper dao;
    @Autowired
    private TbPamOverseasApplyMapper tbPamOverseasApplyMapper;

	/**
	 * 保存
	 * @author liuqinghua
	 * @date Jan 22, 2021 11:41:13 AM
	 */
    @Override
    //@CacheEvict(cacheNames="TbPamOverseasRegionalDetails", allEntries=true)
    public boolean save(TbPamOverseasRegionalDetails entity) {
        return super.save(entity);
    }

	/**
	 * 通过id获取数据
	 * @author liuqinghua
	 * @date Jan 22, 2021 11:41:13 AM
	 */
    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbPamOverseasRegionalDetails getById(Serializable id) {
        return super.getById(id);
    }

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Jan 22, 2021 11:41:13 AM
	 */
    @Override
    //@CacheEvict(cacheNames="TbPamOverseasRegionalDetails", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

	/**
	 * 批量删除数据
	 * @author liuqinghua
	 * @date Jan 22, 2021 11:41:13 AM
	 */
    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

	/**
	 * 分页查询数据
	 * @author liuqinghua
	 * @date Jan 22, 2021 11:41:13 AM
	 */
    //@Cacheable(key = "#data",sync = true)
    public IPage<TbPamOverseasRegionalDetails> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbPamOverseasRegionalDetails entity = JSON.parseObject(JSON.toJSONString(data), TbPamOverseasRegionalDetails.class);
        Page<TbPamOverseasRegionalDetails> page = new Page(currentPage,pageSize);
        QueryWrapper<TbPamOverseasRegionalDetails> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

	/**
	 * 查询所有数据
	 * @author liuqinghua
	 * @date Jan 22, 2021 11:41:13 AM
	 */
    @Override
    //@Cacheable(value = "TbPamOverseasRegionalDetails",sync = true)
    public List<TbPamOverseasRegionalDetails> selectAll() {
        return super.selectAll();
    }

    public IPage<Map<String,Object>> monitorList(MonitorListDto param) {
        // 获取专利模块海外地区时间详情表
        Page<Map<String,Object>> page=new Page(param.getPageNum(),param.getPageSize());
        // 获取专利模块海外地区时间详情表全部数据
        List<TbPamOverseasRegionalDetailsQuery> list = dao.selectListAll(param.getRegionalDetailsId());
        Integer type = param.getType();
        IPage<Map<String,Object>> timeLimitName = dao.getTimeLimitName(page, list, type,param);
        List<Map<String,Object>> records = timeLimitName.getRecords();
        if (type==0) {
            for (Map<String,Object> record : records) {
                Date timeLimitDate = (Date) record.get("timeLimitDate");
                int reminderPoint = Integer.parseInt(String.valueOf(record.get("reminderPoint")));
//                Date timeLimitDate = record.getTimeLimitDate();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(timeLimitDate);
                calendar.add(Calendar.MONTH, reminderPoint);
                if (new Date().before(calendar.getTime())) {
//                    record.setPromptMode(null);
                    record.put("promptMode",null);
                }
            }
        }
        // 获取已立案国家
        if(records.size()> 0) {
            List<String> disclosureIdList = records.stream().map(item -> String.valueOf(item.get("disclosureId"))).collect(Collectors.toList());
            if (disclosureIdList.size()>0) {
            List<Map<String, String>> countryByDisclosureIds = tbPamOverseasApplyMapper.getCountryByDisclosureIds(disclosureIdList);
            List<Map<String, Object>> collect = records.stream().map(m -> {
                countryByDisclosureIds.stream().filter(m2 -> String.valueOf(m.get("disclosureId")).equals(m2.get("disclosureId")))
                        .forEach(s -> {
                            String countryName = Joiner.on(",").join(Arrays.asList(s.get("countryName").split(","))
                                    .stream().sorted(Comparator.reverseOrder()).distinct().collect(Collectors.toList()));
                            m.put("countryName", countryName);
                            m.put("updateDate", s.get("updateDate"));
                        });
                return m;
            }).collect(Collectors.toList());
            timeLimitName.setRecords(collect);
        }
        }
        return timeLimitName;
    }

    // 国内案件提醒数量
    public int monitorListNum(Integer type) {
         // 获取专利模块海外地区时间详情表全部数据
        List<TbPamOverseasRegionalDetailsQuery> list = dao.selectListAll(null);
       return dao.monitorListNum(type,list);
    }

    // 获取案件提醒类型
    public List<Map<String,Object>> getMonitorType() {
        return dao.getMonitorType();
    }
}
